package com.tianqingwl.gsystem.content.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianqingwl.gsystem.common.vo.CommonResultVo;
import com.tianqingwl.gsystem.common.vo.PageResultVo;
import com.tianqingwl.gsystem.content.pojo.ContentType;
import com.tianqingwl.gsystem.content.service.ContentTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/manage/content-tag")
@PreAuthorize("@gs.check('content_tag')")
public class ContentTagController {

    @Autowired
    private ContentTypeService contentTypeService;

    @GetMapping("/content-tag-manage.html")
    public String contentTypePage() {
        return "manage/content/content-type-manage";
    }

    @GetMapping("/content-tag-edit.html")
    public String contentTypeEdit(Model model, Long typeId) {
        if (typeId != null) {
            ContentType contentType = contentTypeService.getContentTypeById(typeId);
            if (contentType != null) {
                model.addAttribute("contentType", contentType);
            }
        }
        return "manage/content/content-tag-edit";
    }

    @GetMapping("/list")
    @ResponseBody
    public Object list(Long page, Long limit, String typeName,Long contentId) {
        page = page == null ? 1 : page;
        limit = limit == null ? 10 : limit;
        Page<ContentType> contentTypePage = new Page<>(page, limit);
        contentTypeService.listContentTypePage(contentTypePage, typeName, 2,contentId);

        List<ContentType> contentTypeList = contentTypePage.getRecords();

        return PageResultVo.success(contentTypePage.getTotal(), contentTypePage.getPages(), contentTypeList);
    }
    @GetMapping("/contentTagList")
    @ResponseBody
    public Object contentTagList(Long page, Long limit,Long contentId) {
        page = page == null ? 1 : page;
        limit = limit == null ? 10 : limit;
        Page<ContentType> contentTypePage = new Page<>(page, limit);
        if (contentId==null){
            return CommonResultVo.success();
        }
        contentTypeService.listContentTypePage(contentTypePage,contentId);

        List<ContentType> contentTypeList = contentTypePage.getRecords();

        return PageResultVo.success(contentTypePage.getTotal(), contentTypePage.getPages(), contentTypeList);
    }

    @PostMapping({"/tagSave"})
    @ResponseBody
    public Object typeSave(ContentType contentType) {
        if (contentType == null) {
            return CommonResultVo.fail("未传入数据");
        }
        contentType.setType(2);
        try {
            //唯一性校验
            boolean exists = contentTypeService.checkNameExists(contentType);
            if (exists) {
                return CommonResultVo.fail("标签名称不能重复");
            }
            ContentType saveResult = contentTypeService.save(contentType);
            return CommonResultVo.success(saveResult);
        } catch (Exception e) {
            return CommonResultVo.fail(e.getMessage());
        }
    }

    @GetMapping("/delete")
    @ResponseBody
    public Object delete(Long[] contentTypeIds, Long typeId) {
        try {
            Integer type = 2;
            if (typeId != null) {
                //标签可直接删除
                contentTypeService.deleteById(typeId, type);
            } else if (contentTypeIds != null && contentTypeIds.length > 0) {
                contentTypeService.deleteByIds(Arrays.asList(contentTypeIds), type);
            } else {
                return CommonResultVo.fail("no id");
            }
            return CommonResultVo.success();
        } catch (Exception e) {
            return CommonResultVo.fail(e.getMessage());
        }
    }
}
